<?php	
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_member extends _init_model{
		public $MEM_ID;
		public $MEM_TITLE;
		public $MEM_USERNAME;
		public $MEM_PASS;
		public $MEM_REGIS_DATE;
		public $MEM_ADDRESS;
		public $MEM_TEL;
		public $MEM_EMAIL;
		public $MEM_GENDER;
		public $MEM_BIRTH_DATE;
		public $MEM_FB_UID;	
		public $MEM_CAF_FAVORITE;	
		
		function __construct(){
			parent::__construct();
			$this->tableName = 'member';
		}		
				
		function check_if_member_registered_on_facebook($facebook_uid){
			$this->db->where('MEM_FB_UID', $facebook_uid);
			$count = $this->db->count_all_results($this->tableName);

			if ($count > 0)
				return true;
			else
				return false;
		}
		function check_member_exist($member_id){
			$this->db->where('MEM_ID', $member_id);
			$count = $this->db->count_all_results($this->tableName);
			if ($count > 0)
				return true;
			else
				return false;
		}
		function get_member_by_facebook_uid($facebook_uid){
			$this->db->select(array('MEM_ID','MEM_TITLE',));
			$this->db->where('MEM_FB_UID', $facebook_uid);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row_array(0);
			else
				return null;
		}
		
		function get_member($member_id, $fields=null){
			if (!is_null($fields))
				$this->db->select($fields);
			
			$this->db->where('MEM_ID', $member_id);
			$query = $this->db->get($this->tableName);

			if ($query->num_rows() > 0){
				$return = $query->row_array(0);
			
				if (isset($return['MEM_FB_UID']) && !empty($return['MEM_FB_UID'])){
					$this->load->library('facebook_api', array(0 => false));
					$return['MEM_AVATAR'] = $this->facebook_api->get_user_avatar($return['MEM_FB_UID']);
				}
				
				return $return;
			} else				
				return null;			
		}		
		function get_member_list($start_index=null, $end_index=null, $desc=true){
			if (!is_null($start_index) && !is_null($end_index))								
				$query = $this->db->get($this->tableName, $end_index, $start_index);
			else
				$query = $this->db->get($this->tableName);
						
			$return = $query->result_array();
			if (count($return) > 0){
				
				$this->load->library('facebook_api', array(0 => false));
				for ($i=0; $i<count($return); $i++)					
					$return[$i]['MEM_AVATAR'] = $this->facebook_api->get_user_avatar($return[$i]['MEM_FB_UID']);				
			}
			return $return;
		}	
		function get_member_list_for_dropdown(){
			$this->db->select(array('MEM_ID','MEM_TITLE'));
			$query = $this->db->get($this->tableName);			
						
			$return = array();
			if ($query->num_rows() > 0){
				$return[-1] = '';
				foreach ($query->result_array() as $row){
					$return[$row['MEM_ID']] = $row['MEM_USERNAME'] . ' - ' . $row['MEM_TITLE'];
				}
			}			
			return $return;
		}
		function get_member_authen_by_facebook_uid($uid){
			$sqlcmd = 'SELECT MEM_TITLE,MEM_ID FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE MEM_FB_UID = ' . $uid;
			$query = $this->db->query($sqlcmd);
			
			if ($query->num_rows() > 0)
				return array('MEM_ID' => $query->row(0)->MEM_ID,
								'MEM_TITLE' => $query->row(0)->MEM_TITLE);
			else
				return null;
		}
		
		// favorite cafe
		function get_favorite_cafe($member_id, $display_length=null, $start_index=null, $field=null){
			$result = $this->get_member($member_id, array('MEM_CAF_FAVORITE'));			
			$this->db->flush_cache();			
			
			if (!is_null($field))
				$this->db->select($field);				
			else
				$this->db->select(array('CAF_CODE','CAF_NAME','CAF_TITLE'));

			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID','left');							
			$this->db->where_in('cafe.CAF_ID', explode(',', $result['MEM_CAF_FAVORITE']));
			$this->db->order_by('CAF_LAST_ACTIVE', 'DESC');
			
			if ((!is_null($display_length) || !empty($display_length)) && 
				(!is_null($start_index) || !empty($start_index)))
				
				$query = $this->db->get('cafe', $display_length, $start_index);
			else
				$query = $this->db->get('cafe');
			
			$return = $query->result_array();
			if (count($return) > 0){
				$this->load->library('define/directory_path');
				
				for ($i=0; $i<count($return); $i++){
					
						if (!empty($return[$i]['CAF_IMG_L_FILENAME']))
							$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
						else
							$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
					
				}
			}

			return $return;		
		}
		function get_favorite_cafe_count($member_id){
			$result = $this->get_member($member_id, array('MEM_CAF_FAVORITE'));
			$this->db->flush_cache();
			
			return count(explode(',', $result['MEM_CAF_FAVORITE']));
		}
		function add_favorite_cafe($cafe_id, $member_id){			
			$fav_result = $this->get_favorite_cafe($member_id, null, null, array('cafe.CAF_ID'));

			// generate array
			$fav_arr = array();
			for ($i=0; $i<count($fav_result); $i++)
				array_push($fav_arr, $fav_result[$i]['CAF_ID']);			
			
			sort($fav_arr);
			
			// push array
			if (!in_array($cafe_id, $fav_arr))
				array_push($fav_arr, $cafe_id);

			$this->db->where('MEM_ID', $member_id);
			return $this->db->update($this->tableName, array('MEM_CAF_FAVORITE' => implode(',', $fav_arr)));
		}
		function remove_favorite_cafe($cafe_id, $member_id){
			$fav_result = $this->get_favorite_cafe($member_id, null, null, array('cafe.CAF_ID'));

			// generate array
			$fav_arr = array();
			for ($i=0; $i<count($fav_result); $i++)
				array_push($fav_arr, $fav_result[$i]['CAF_ID']);			
			
			sort($fav_arr);
			
			// remove array
			if (in_array($cafe_id, $fav_arr))
				$fav_arr = array_diff($fav_arr, array($cafe_id));				
				
			$this->db->where('MEM_ID', $member_id);
			return $this->db->update($this->tableName, array('MEM_CAF_FAVORITE' => implode(',', $fav_arr)));
		}
		
		function authen($username, $password) {
			$sqlcmd = 'SELECT MEM_ID, MEM_TITLE';
			$sqlcmd .= ' FROM ' . $this->tableName;				
			$sqlcmd .= ' WHERE MEM_EMAIL = ?';
			$sqlcmd .= ' AND MEM_PASS = ?';		
			
			$query = $this->db->query($sqlcmd, array(strtolower($username), $password));			
			if ($query->num_rows() > 0)			
				return array('MEM_ID' => $query->row(0)->MEM_ID,
								'MEM_TITLE' => $query->row(0)->MEM_TITLE);							
			else
				return null;			
		}
		function change_password($member_id, $password, $old_password=null){
			$old_pass = true;
			
			$this->load->library('string_function');			
			if (!empty($old_password) && !is_null($old_password) && $old_password != ''){
				
				$this->db->select('MEM_PASS');
				$this->db->where(array('MEM_ID' => $member_id));
				$query = $this->db->get($this->tableName);
				
				if ($query->num_rows() > 0){
					$this->db->flush_cache();
					
					if (!empty($query->row(0)->MEM_PASS)){						
						$this->db->where(array('MEM_PASS' => $this->string_function->provide_password($old_password),
												'MEM_ID' => $member_id));
						$count = $this->db->count_all_results($this->tableName);
						if ($count <= 0)
							$old_pass = false;	
							
						$this->db->flush_cache();
					}
				} 				
			}
			
			if ($old_pass){
				$this->db->where('MEM_ID', $member_id);
				return $this->db->update($this->tableName, array('MEM_PASS' => $this->string_function->provide_password($password)));
			} else
				return -1;
		}
		
		function add(){
			$data = array(
				'MEM_TITLE' => $this->MEM_TITLE,
				'MEM_PASS' => $this->MEM_PASS,
				'MEM_REGIS_DATE' => $this->MEM_REGIS_DATE,
				'MEM_ADDRESS' => $this->MEM_ADDRESS,
				'MEM_TEL' => $this->MEM_TEL,
				'MEM_EMAIL' => $this->MEM_EMAIL,
				'MEM_GENDER' => $this->MEM_GENDER,
				'MEM_BIRTH_DATE' => $this->MEM_BIRTH_DATE,
				'MEM_FB_UID' => $this->MEM_FB_UID
			);
			return $this->db->insert($this->tableName, $data);
		}
		function edit($member_id){
			$data = array();
			
			if (!empty($this->MEM_TITLE))
				$data['MEM_TITLE'] = $this->MEM_TITLE;			
			if (!empty($this->MEM_PASS))
				$data['MEM_PASS'] = $this->MEM_PASS;
			if (!empty($this->MEM_ADDRESS))
				$data['MEM_ADDRESS'] = $this->MEM_ADDRESS;
			if (!empty($this->MEM_TEL))
				$data['MEM_TEL'] = $this->MEM_TEL;
			if (!empty($this->MEM_EMAIL))
				$data['MEM_EMAIL'] = $this->MEM_EMAIL;
			if (!empty($this->MEM_PERSON_ID))
				$data['MEM_PERSON_ID'] = $this->MEM_PERSON_ID;
			if (!empty($this->MEM_GENDER))
				$data['MEM_GENDER'] = $this->MEM_GENDER;
			if (!empty($this->MEM_BIRTH_DATE))
				$data['MEM_BIRTH_DATE'] = $this->MEM_BIRTH_DATE;
			
			$this->db->where('MEM_ID', $member_id);
			return $this->db->update($this->tableName, $data);			
		}
	}
?>